﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YemekteyizLib.Bs;
using YemekteyizLib;
using System.Web.Security;
using System.Collections.Specialized;

public partial class restoranDetay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Restoran r = new Restoran();
        r.ID = Convert.ToInt32(Request.QueryString["restID"]);
        if (!IsPostBack)
        {
            dtlGruplar.DataSource = GrupProvider.GetByRestoran(r);
            dtlGruplar.DataBind();
        }



    }

    protected void dtlGruplar_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataList d = e.Item.FindControl("dtListUrunler") as DataList;
        d.ItemDataBound += new DataListItemEventHandler(d_ItemDataBound);
        Grup g = e.Item.DataItem as Grup;
        d.DataSource = g.Urunleri;
        d.DataBind();

        
    }

    void d_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        (e.Item.FindControl("TextBox1") as TextBox).Text = "1";
    }
    
    protected void dtListUrunler_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ekle")
        {
            Uye u = Session["uye"] as Uye;
            if (u == null)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                int adet = 1;
                try
                {
                    adet = Convert.ToInt32((e.Item.FindControl("TextBox1") as TextBox).Text);
                }
                catch (Exception)
                {
                    Response.Write("hata");
                    
                }

                SepetProvider.UrunEkle(u.AktifSepet, UrunProvider.GetByID(Convert.ToInt32(e.CommandArgument)), adet);
                DataList d = this.Master.FindControl("DataList1") as DataList;
                SepetDoldur(d);

                

            }
            
        }
    }

    public void SepetDoldur(DataList d)
    {
        Uye u = Session["uye"] as Uye;

        u.AktifSepet = SepetProvider.GetAktifByUye(u);
        Session["uye"] = u;

        d.DataSource = u.AktifSepet.Urunleri;
        d.DataBind();
    }

    
}
